package com.itheima.controller;

import com.itheima.entity.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/*
类简介：

功能：

时间：2020/4/11

*/
@Controller
@RequestMapping("/user")
@ResponseBody
public class UserController {

    @RequestMapping("getUsername")
    public Result getUsername(){

        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            User user = (User)authentication.getPrincipal();
            return new Result(true,"",user.getUsername());
        }catch (Exception e){
            return new Result(false,"");
        }

    }

}
